<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 上午 10:11
 */

namespace App\Common;

class Menu
{
    private static $interface;

    static public function create()
    {
        if (empty(static::$interface)) {
            static::$interface = new static();
        }
        return static::$interface;
    }

    public function toTree($data, $id = 'id', $parent_id = 'parent_id', $pid = 0, $laraevl = 0)
    {
        $arr = [];
        foreach ($data as $key => $value) {
            if ($value[$parent_id] == $pid) {
                $arr[$key] = $value;
                $arr[$key]['laraevl'] = $laraevl;
                $arr[$key]['son'] = self::toTree($data, $id, $parent_id, $value['id'], $laraevl + 1);
            }
        }
        return collect($arr);
    }
}